﻿(function ($) {
    var dialogLanguageSettings = {
        'width': 600,
        'title': 'Select / Add a Language',
        'LanguageCode': null,
        'LanguageId': null,
        'resizable': true,
        'position': [($(window).size.width / 2) / (this.width/2), 10],
        'AutoCompleteElement': null,
        'ApplicationMessagesPartial': null,
        'InputElement': null
    };

    var methods = {
        init: function (options) {

            return this.each(function () {
                var that = $(this);

                if (options) {
                    $.extend(dialogLanguageSettings, options);
                }

                $('#' + dialogLanguageSettings.btn).button({
                    icons: {
                        primary: 'ui-icon ui-icon-zoomin'
                    },
                    text: false
                }).bind('click', function (e) {
                    dialogLanguageSettings.LanguageCode = $("#" + dialogLanguageSettings.InputElement).val();
                    dialogLanguageSettings.LanguageId = $("#" + dialogLanguageSettings.InputElement).attr("data-opus-id");
                    methods.open.call(that);
                    e.preventDefault();
                });

                $("#" + dialogLanguageSettings.AutoCompleteElement).opusAutoComplete({
                    url: '/Ref/Languages'
                });

            });
        },
        destroy: function () {
            return this.each(function () {
                $(this).unbind('.selectLanguage');
            });
        },
        open: function (options) {
            if (options) {
                $.extend(dialogLanguageSettings, options);
            }

            //return this.each(function () {
            var d = $(this).dialog({
                autoOpen: true,
                width: dialogLanguageSettings.width,
                position: dialogLanguageSettings.position,
                resizable: dialogLanguageSettings.resizable,
                title: dialogLanguageSettings.title,
                modal: true,
                open: function (event, ui) {
                    $(this).load('/Ref/LanguageSelect' + '?id=' + dialogLanguageSettings.LanguageId);
                },
                buttons: {
                    "Select": function () {
                        if ($("#Dialog_Language_Clear").hasClass('CancelButton')) {
                            alert("Please save or cancel the current operation first");
                        }
                        else {
                            if ($("#Dialog_Language_Description").val() != "") {
                                $.get('/Shared/ApplicationMessages', {}, function (view) {
                                    $("#" + dialogLanguageSettings.ApplicationMessagesPartial).html(view);
                                });
                                $("#" + dialogLanguageSettings.InputElement)
                                    .attr('data-opus-id', $("#Dialog_Language_Id").val())
                                    .attr('data-opus-code', $("#Dialog_Language_Code").val())
                                    .val($("#Dialog_Language_Description").val())
                                $(this).dialog("close");
                            }
                            else {
                                alert("Please select a language first or close the dialog");
                            }
                        }
                    },
                    "Close": function () {
                        if ($("#Dialog_Language_Clear").hasClass('CancelButton')) {
                            alert("Please save or cancel the current operation first");
                        }
                        else {
                            $.get('/Shared/ApplicationMessages', {}, function (view) {
                                $("#" + dialogLanguageSettings.ApplicationMessagesPartial).html(view);
                            });
                            $(this).dialog("close");
                        }
                    }
                }
            });

            return this;
            //});
        }
    };

    $.fn.selectLanguage = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.selectLanguage');
        }
    };
})(jQuery);
